/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.cholm.media.castdr.adapters;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import org.cholm.media.castdr.LogUtils;
import org.cholm.media.castdr.backend.Category;
import org.cholm.media.castdr.backend.Extractor;
import org.cholm.media.castdr.fragments.SubFragment;

import java.util.List;

import static org.cholm.media.castdr.LogUtils.LOGd;
/**
 * Adapter used to load ViewPager
 *
 * @author cholm
 */
public class TopAdapter extends FragmentPagerAdapter {
    /**
     * Debug tag
     */
    private static final String TAG = LogUtils.makeLogTag(TopAdapter.class);

    /**
     * Constructor
     *
     * @param fm Fragment manage
     */
    public TopAdapter(FragmentManager fm) {
        super(fm);
    }

    /**
     * Get our data
     *
     * @return
     */
    private List<Category> getData() {
        return Extractor.instance().getList();
    }

    /**
     * Get our sub-category
     *
     * @param position Position
     * @return Sub-category or null
     */
    public Category getCategory(int position) {
        return getData().get(position);
    }

    /**
     * Get an item
     */
    @Override
    public Fragment getItem(int arg0) {
        Category cat = getCategory(arg0);
        String name = cat.getName();

        LOGd(TAG, "Creating a new fragment for " + name);
        SubFragment ret = new SubFragment();
        ret.setCategoryId(arg0);
        // mFrags.put(name, frag);
        return ret;
    }

    /**
     * Number of sub-categories
     */
    @Override
    public int getCount() {
        return getData().size();
    }

    /**
     * Page title
     */
    @Override
    public CharSequence getPageTitle(int position) {
        return getCategory(position).getName();
    }
}
// EOF
